﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Dice;
using AnguaRPGEngine.Library.Systems;
using System.IO;

namespace AnguaRPGEngine.Library.Forms
{
    public partial class DiceEditor : Form
    {
        public DiceEditor()
        {
            InitializeComponent();
            btnPipColor.BackColor = Color.White;
        }

        private void btnPipColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = btnPipColor.BackColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                btnPipColor.BackColor = colorDialog1.Color;
            }
            lblD10.ForeColor = lblD100.ForeColor = lblD12.ForeColor = lblD20.ForeColor = lblD4.ForeColor = lblD6.ForeColor = lblD8.ForeColor = btnPipColor.BackColor;
        }

        private void pnlD4_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Four-Sided Die";
            openFileDialog1.FileName = "D4.png";
            openFileDialog1.Filter = "D4 (D4.png)|D4.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD4.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }
        }

        private void pnlD6_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Six-Sided Die";
            openFileDialog1.FileName = "D6.png";
            openFileDialog1.Filter = "D6 (D6.png)|D6.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD6.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }
        }

        private void pnlD8_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Eight-Sided Die";
            openFileDialog1.FileName = "D8.png";
            openFileDialog1.Filter = "D8 (D8.png)|D8.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD8.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }

        }

        private void pnlD10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Ten-Sided Die";
            openFileDialog1.FileName = "D10.png";
            openFileDialog1.Filter = "D10 (D10.png)|D10.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD10.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }

        }

        private void pnlD12_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Twelve-Sided Die";
            openFileDialog1.FileName = "D12.png";
            openFileDialog1.Filter = "D12 (D12.png)|D12.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD12.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }

        }

        private void pnlD20_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Twenty-Sided Die";
            openFileDialog1.FileName = "D20.png";
            openFileDialog1.Filter = "D20 (D20.png)|D20.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD20.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }

        }

        private void pnlD100_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Percentile Die";
            openFileDialog1.FileName = "D100.png";
            openFileDialog1.Filter = "D100 (D100.png)|D100.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pnlD100.BackgroundImage = new Bitmap(Image.FromFile(openFileDialog1.FileName));
            }

        }

        private void DiceEditor_Load(object sender, EventArgs e)
        {
            lblD10.ForeColor = lblD100.ForeColor = lblD12.ForeColor = lblD20.ForeColor = lblD4.ForeColor = lblD6.ForeColor = lblD8.ForeColor = btnPipColor.BackColor;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (DieRoller.Instance.DiceCollection.Dice.FindAll(p => p.Name == txtName.Text).Count > 0)
                {
                    throw new Exception("A Dice Set By That Name Already Exists.");
                }
                if (DieRoller.Instance.DiceCollection.Dice.FindAll(p => p.PathName == txtPath.Text).Count > 0)
                {
                    throw new Exception("A Dice Set Already Exists In The Specified Path.");
                }

                string filePath = lblCompletePath.Text;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                try
                {
                    if (File.Exists(filePath + "D4.png"))
                    {
                        File.Delete(filePath + "D4.png");
                    }
                    pnlD4.BackgroundImage.Save(filePath + "D4.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D6.png"))
                    {
                        File.Delete(filePath + "D6.png");
                    }
                    pnlD6.BackgroundImage.Save(filePath + "D6.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D8.png"))
                    {
                        File.Delete(filePath + "D8.png");
                    }
                    pnlD8.BackgroundImage.Save(filePath + "D8.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D10.png"))
                    {
                        File.Delete(filePath + "D10.png");
                    }
                    pnlD10.BackgroundImage.Save(filePath + "D10.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D12.png"))
                    {
                        File.Delete(filePath + "D12.png");
                    }
                    pnlD12.BackgroundImage.Save(filePath + "D12.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D20.png"))
                    {
                        File.Delete(filePath + "D20.png");
                    }
                    pnlD20.BackgroundImage.Save(filePath + "D20.png");
                }
                catch
                {
                }
                try
                {
                    if (File.Exists(filePath + "D100.png"))
                    {
                        File.Delete(filePath + "D100.png");
                    }
                    pnlD100.BackgroundImage.Save(filePath + "D100.png");
                }
                catch
                {
                }
                DiceDescription dd = new DiceDescription();
                dd.Name = txtName.Text;
                dd.PathName = txtPath.Text;
                dd.PipColor = btnPipColor.BackColor.ToArgb();

                DieRoller.Instance.DiceCollection.Dice.Add(dd);
                DieRoller.Instance.SaveDiceCollection();
                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(this, string.Format("ERROR: {0}", err.Message), "Failed To Save Dice Set");
            }
        }

        private void txtPath_TextChanged(object sender, EventArgs e)
        {
            lblCompletePath.Text = string.Format("{0}Dice\\{1}\\",GameSystem.Instance.PathToData, txtPath.Text.Replace(".", "\\"));
        }

    }
}